package com.jshongli.myapplication.common.callback;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.internal.$Gson$Types;
import com.jshongli.myapplication.MyApplication;
import com.jshongli.myapplication.common.utils.AppManager;
import com.jshongli.myapplication.common.utils.ToastUtils;
import com.jshongli.myapplication.model.base.AppBean;
import com.zhy.http.okhttp.callback.Callback;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/6/21.
 */

public abstract class MyCallback<T extends AppBean> extends Callback<T> {

    @Override
    public T parseNetworkResponse(Response response, int id) throws Exception {
        String strContent = response.body().string();
        if (!TextUtils.isEmpty(strContent)) {
            try {
                Gson gson = new Gson();
                T t = gson.fromJson(strContent, mType);
                Log.e("AppBean:", t.toString());
                return t;
            } catch (JsonParseException e) {//Json解析的错误
                Log.e("MyCallback", "JsonParseException: " + e.toString());
                ToastUtils.show(mContext, "数据解析错误");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public void onError(Call call, Exception e, int id) {
        ToastUtils.show(mContext, e.getMessage());
    }

    @Override
    public void onResponse(T response, int id) {

        if (response != null) {
            if (response.success)
                onSuccess(response);
            else if(!response.success && response.code == -1){
//                ToastUtils.show(MyApplication.getInstance(), "程序出错");
                onFail(response);
            }else if (!response.result && "请登录后再访问接口".equals(response.msg)) {
                AppManager.exceptionExitToLogin(mContext, "登录过期，请重新登录");
            } else {
//                ToastUtils.show(mContext, response.msg);
                onFail(response);
            }
        } else {
            ToastUtils.show(MyApplication.getInstance(), "解析错误");
            onResponseFail();
        }

    }

    public abstract void onSuccess(T response);

    public void onFail(T response) {
    }

    public void onResponseFail() {
    }

    public MyCallback(Context context) {
        this.mContext = context;
        mType = getSuperclassTypeParameter(getClass());
    }

    private Context mContext;
    public Type mType;

    /**
     * 通过反射想要的返回类型
     *
     * @param subclass
     * @return
     */
    static Type getSuperclassTypeParameter(Class<?> subclass) {
        Type superclass = subclass.getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
        return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
    }


}
